package com.akk.module.access.service;

import com.akk.module.access.entity.*;
import com.akk.module.access.mapper.AccessBankMapper;
import com.akk.module.access.mapper.AccessBindMapper;
import com.akk.module.access.mapper.AccessMapper;
import com.akk.module.avs.mapper.dao.AvsPayAccessDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AccessService {

    @Autowired
    AccessMapper accessMapper;
    @Autowired
    AccessExample accessExample;

    @Autowired
    AccessBankMapper accessBankMapper;
    @Autowired
    AccessBankExample accessBankExample;

    @Autowired
    AccessBindMapper accessBindMapper;
    @Autowired
    AccessBindExample accessBindExample;
    @Autowired
    private AvsPayAccessDao payAccessDao;

    public List<Access> findByGateway(String gateway) {
        accessExample.clear();
        accessExample.createCriteria().andAccessGatewayEqualTo(gateway);
        List<Access> accesss = accessMapper.selectByExample(accessExample);
        return accesss;
    }

    public List<AccessBank> findAccessBankByAccessId(String accessId){
        accessBankExample.createCriteria();
        accessBankExample.createCriteria().andAccessIdEqualTo(accessId);
        return accessBankMapper.selectByExample(accessBankExample);
    }

    public List<AccessBank> findAccessBankByBankName(String bankName){
        accessBankExample.createCriteria();
        accessBankExample.createCriteria().andBankNameEqualTo(bankName);
        return accessBankMapper.selectByExample(accessBankExample);
    }

    public List<AccessBank> selectAllBank(){
        accessBankExample.clear();
        accessBankExample.createCriteria();
        return accessBankMapper.selectByExample(accessBankExample);
    }

    public Access findByAccessId(String accessId) {
        return accessMapper.selectByPrimaryKey(accessId);
    }

    public List<AccessBind> findAccessBindByPayerIdAndAccessId(String payerId,String accessId){
        accessBindExample.clear();
        accessBindExample.createCriteria().andAccessIdEqualTo(accessId).andPayerIdEqualTo(payerId);
        return accessBindMapper.selectByExample(accessBindExample);
    }

    public List<AccessBind> findByBindId(String bindId){
        accessBindExample.clear();
        accessBindExample.createCriteria().andBindIdEqualTo(bindId);
        return accessBindMapper.selectByExample(accessBindExample);
    }

    public List<AccessBind> findByPayerId(String payerId){
        accessBindExample.clear();
        accessBindExample.createCriteria().andPayerIdEqualTo(payerId).andCardNumIsNotNull();
        return accessBindMapper.selectByExample(accessBindExample);
    }

    public List<AccessBind> findByPayerIdAndGateway(String payerId,String gateway){
        return payAccessDao.findByPayerIdAndGateway(payerId, gateway);
    }

    public List<AccessBind> findByParams(Map params) {
        accessBindExample.clear();
        AccessBindExample.Criteria criteria = accessBindExample.createCriteria();
        if (params.containsKey("payerId"))
            criteria.andPayerIdEqualTo(params.get("payerId").toString());
        if (params.containsKey("cardAfterFour"))
            criteria.andLastFourNumEqualTo(params.get("cardAfterFour").toString());
        if (params.containsKey("cardType"))
            criteria.andCardTypeEqualTo(params.get("cardType").toString());
        if (params.containsKey("bankCode"))
            criteria.andCardCodeEqualTo(params.get("bankCode").toString());
        if (params.containsKey("bindId"))
            criteria.andBindIdEqualTo(params.get("bindId").toString());
        if (params.containsKey("cardNum"))
            criteria.andCardNumEqualTo(params.get("cardNum").toString());
        return accessBindMapper.selectByExample(accessBindExample);
    }

    public List<AccessBind> findByPayerIdAndToken(String payerId,String token){
        accessBindExample.clear();
        accessBindExample.createCriteria().andPayerIdEqualTo(payerId).andTokenEqualTo(token);
        return accessBindMapper.selectByExample(accessBindExample);
    }

    public AccessBind findAccessBindById(String accessBindId){
        return accessBindMapper.selectByPrimaryKey(accessBindId);
    }

    public List<AccessBind> findAccessBindByBindId(String bindId){
        accessBindExample.clear();
        accessBindExample.createCriteria().andBindIdEqualTo(bindId);
        return accessBindMapper.selectByExample(accessBindExample);
    }

    public List<AccessBank> findByAccessIdAndBankCode(String accessId, String bankCode) {
        accessBankExample.clear();
        accessBankExample.createCriteria().andAccessIdEqualTo(accessId).andBankCodeEqualTo(bankCode);
        return accessBankMapper.selectByExample(accessBankExample);
    }

    public void save(AccessBind accessBind) {
        accessBindMapper.insert(accessBind);
    }

    public void updateBind(AccessBind accessBind){
        accessBindMapper.updateByPrimaryKey(accessBind);
    }

    public void deleteBind(String payerId,String cardNum){
        accessBindExample.clear();
        accessBindExample.createCriteria().andPayerIdEqualTo(payerId).andCardNumEqualTo(cardNum);
        accessBindMapper.deleteByExample(accessBindExample);
    }
}
